<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: dialog.cpp Example File (sql/masterdetail/dialog.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">dialog.cpp Example File<br /><sup><sup>sql/masterdetail/dialog.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2006-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &quot;dialog.h&quot;

 int uniqueAlbumId;
 int uniqueArtistId;

 Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
                QFile *output, QWidget *parent)
      : QDialog(parent)
 {
     model = albums;
     albumDetails = details;
     outputFile = output;

     QGroupBox *inputWidgetBox = createInputWidgets();
     QDialogButtonBox *buttonBox = createButtons();

     QVBoxLayout *layout = new QVBoxLayout;
     layout-&gt;addWidget(inputWidgetBox);
     layout-&gt;addWidget(buttonBox);
     setLayout(layout);

     setWindowTitle(tr(&quot;Add Album&quot;));
 }

 void Dialog::submit()
 {
     QString artist = artistEditor-&gt;text();
     QString title = titleEditor-&gt;text();

     if (artist.isEmpty() || title.isEmpty()) {
         QString message(tr(&quot;Please provide both the name of the artist &quot;
                            &quot;and the title of the album.&quot;));
         QMessageBox::information(this, tr(&quot;Add Album&quot;), message);
     } else {
         int artistId = findArtistId(artist);
         int albumId = addNewAlbum(title, artistId);

         QStringList tracks;
         tracks = tracksEditor-&gt;text().split(&quot;,&quot;, QString::SkipEmptyParts);
         addTracks(albumId, tracks);

         increaseAlbumCount(indexOfArtist(artist));
         accept();
     }
 }

 int Dialog::findArtistId(const QString &amp;artist)
 {
     QSqlTableModel *artistModel = model-&gt;relationModel(2);
     int row = 0;

     while (row &lt; artistModel-&gt;rowCount()) {
         QSqlRecord record = artistModel-&gt;record(row);
         if (record.value(&quot;artist&quot;) == artist)
             return record.value(&quot;id&quot;).toInt();
         else
             row++;
     }
     return addNewArtist(artist);
 }

 int Dialog::addNewArtist(const QString &amp;name)
 {
     QSqlTableModel *artistModel = model-&gt;relationModel(2);
     QSqlRecord record;

     int id = generateArtistId();

     QSqlField f1(&quot;id&quot;, QVariant::Int);
     QSqlField f2(&quot;artist&quot;, QVariant::String);
     QSqlField f3(&quot;albumcount&quot;, QVariant::Int);

     f1.setValue(QVariant(id));
     f2.setValue(QVariant(name));
     f3.setValue(QVariant(0));
     record.append(f1);
     record.append(f2);
     record.append(f3);

     artistModel-&gt;insertRecord(-1, record);
     return id;
 }

 int Dialog::addNewAlbum(const QString &amp;title, int artistId)
 {
     int id = generateAlbumId();
     QSqlRecord record;

     QSqlField f1(&quot;albumid&quot;, QVariant::Int);
     QSqlField f2(&quot;title&quot;, QVariant::String);
     QSqlField f3(&quot;artistid&quot;, QVariant::Int);
     QSqlField f4(&quot;year&quot;, QVariant::Int);

     f1.setValue(QVariant(id));
     f2.setValue(QVariant(title));
     f3.setValue(QVariant(artistId));
     f4.setValue(QVariant(yearEditor-&gt;value()));
     record.append(f1);
     record.append(f2);
     record.append(f3);
     record.append(f4);

     model-&gt;insertRecord(-1, record);
     return id;
 }

 void Dialog::addTracks(int albumId, QStringList tracks)
 {
     QDomElement albumNode = albumDetails.createElement(&quot;album&quot;);
     albumNode.setAttribute(&quot;id&quot;, albumId);

     for (int i = 0; i &lt; tracks.count(); i++) {
         QString trackNumber = QString::number(i);
         if (i &lt; 10)
             trackNumber.prepend(&quot;0&quot;);

         QDomText textNode = albumDetails.createTextNode(tracks.at(i));

         QDomElement trackNode = albumDetails.createElement(&quot;track&quot;);
         trackNode.setAttribute(&quot;number&quot;, trackNumber);
         trackNode.appendChild(textNode);

         albumNode.appendChild(trackNode);
     }

     QDomNodeList archive = albumDetails.elementsByTagName(&quot;archive&quot;);
     archive.item(0).appendChild(albumNode);

<span class="comment"> /*
     The following code is commented out since the example uses an in
     memory database, i.e., altering the XML file will bring the data
     out of sync.

     if (!outputFile-&gt;open(QIODevice::WriteOnly)) {
         return;
     } else {
         QTextStream stream(outputFile);
         archive.item(0).save(stream, 4);
         outputFile-&gt;close();
     }
 */</span>
 }

 void Dialog::increaseAlbumCount(QModelIndex artistIndex)
 {
     QSqlTableModel *artistModel = model-&gt;relationModel(2);

     QModelIndex albumCountIndex;
     albumCountIndex = artistIndex.sibling(artistIndex.row(), 2);

     int albumCount = albumCountIndex.data().toInt();
     artistModel-&gt;setData(albumCountIndex, QVariant(albumCount + 1));
 }

 void Dialog::revert()
 {
     artistEditor-&gt;clear();
     titleEditor-&gt;clear();
     yearEditor-&gt;setValue(QDate::currentDate().year());
     tracksEditor-&gt;clear();
 }

 QGroupBox *Dialog::createInputWidgets()
 {
     QGroupBox *box = new QGroupBox(tr(&quot;Add Album&quot;));

     QLabel *artistLabel = new QLabel(tr(&quot;Artist:&quot;));
     QLabel *titleLabel = new QLabel(tr(&quot;Title:&quot;));
     QLabel *yearLabel = new QLabel(tr(&quot;Year:&quot;));
     QLabel *tracksLabel = new QLabel(tr(&quot;Tracks (separated by comma):&quot;));

     artistEditor = new QLineEdit;
     titleEditor = new QLineEdit;

     yearEditor = new QSpinBox;
     yearEditor-&gt;setMinimum(1900);
     yearEditor-&gt;setMaximum(QDate::currentDate().year());
     yearEditor-&gt;setValue(yearEditor-&gt;maximum());
     yearEditor-&gt;setReadOnly(false);

     tracksEditor = new QLineEdit;

     QGridLayout *layout = new QGridLayout;
     layout-&gt;addWidget(artistLabel, 0, 0);
     layout-&gt;addWidget(artistEditor, 0, 1);
     layout-&gt;addWidget(titleLabel, 1, 0);
     layout-&gt;addWidget(titleEditor, 1, 1);
     layout-&gt;addWidget(yearLabel, 2, 0);
     layout-&gt;addWidget(yearEditor, 2, 1);
     layout-&gt;addWidget(tracksLabel, 3, 0, 1, 2);
     layout-&gt;addWidget(tracksEditor, 4, 0, 1, 2);
     box-&gt;setLayout(layout);

     return box;
 }

 QDialogButtonBox *Dialog::createButtons()
 {
     QPushButton *closeButton = new QPushButton(tr(&quot;&amp;Close&quot;));
     QPushButton *revertButton = new QPushButton(tr(&quot;&amp;Revert&quot;));
     QPushButton *submitButton = new QPushButton(tr(&quot;&amp;Submit&quot;));

     closeButton-&gt;setDefault(true);

     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
     connect(revertButton, SIGNAL(clicked()), this, SLOT(revert()));
     connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));

     QDialogButtonBox *buttonBox = new QDialogButtonBox;
     buttonBox-&gt;addButton(submitButton, QDialogButtonBox::ResetRole);
     buttonBox-&gt;addButton(revertButton, QDialogButtonBox::ResetRole);
     buttonBox-&gt;addButton(closeButton, QDialogButtonBox::RejectRole);

     return buttonBox;
 }

 QModelIndex Dialog::indexOfArtist(const QString &amp;artist)
 {
     QSqlTableModel *artistModel = model-&gt;relationModel(2);

     for (int i = 0; i &lt; artistModel-&gt;rowCount(); i++) {
         QSqlRecord record =  artistModel-&gt;record(i);
         if (record.value(&quot;artist&quot;) == artist)
             return artistModel-&gt;index(i, 1);
     }

     return QModelIndex();
 }

 int Dialog::generateArtistId()
 {
     uniqueArtistId += 1;
     return uniqueArtistId;
 }

 int Dialog::generateAlbumId()
 {
     uniqueAlbumId += 1;
     return uniqueAlbumId;
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
